/**
 * 
 */
package com.depot.auth.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.base.LoginUser;
import com.cache.ResIcon;
import com.cache.ResIconGroup;
import com.depot.auth.dao.AppMenuDao;
import com.depot.domains.AppMenu;
import com.opensymphony.xwork2.ModelDriven;
import com.soar.BaseActionSupport;
import com.soar.cache.CacheManager;
import com.soar.context.Environment;
import com.soar.senv.SoarProperties;

public class AppMenuAction extends BaseActionSupport implements ModelDriven<AppMenu>
{
	private static final long serialVersionUID = -851394301648065534L;

	public AppMenu domain = new AppMenu();

	public HashMap<String, String> appmenu = new HashMap<String, String>();

	public List<HashMap<String, String>> icongroup = new ArrayList<HashMap<String, String>>();

	/**
	 * 应用菜单列表
	 * 
	 * @param env
	 * @param domain
	 * @param action
	 * @return
	 * @throws Exception
	 */
	public String execute(Environment env) throws Exception
	{
		this.sysPageList = AppMenuDao.select(env, domain);
		return "app_menu_list";
	}

	/**
	 * 应用菜单修改
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public String doSave(Environment env) throws Exception
	{
		String result = AppMenuDao.doSave(env, domain);
		if (result.equals("1"))
		{
			this.setMessage("操作处理成功！", env, true);
		}
		else if (result.equals("0"))
		{
			this.setMessage("操作处理失败，请重试！", env, false);
		}
		else if (result.equals("2"))
		{
			this.setMessage("重复的数据，操作处理失败，请修改后再试！", env, false);
		}
		return null;
	}

	public String saveOrder(Environment env) throws Exception
	{
		String result = AppMenuDao.updateOrder(env, domain);
		if (result.equals("1"))
		{
			this.setMessage("操作处理成功！");
		}
		else
		{
			this.setMessage("操作处理失败，请重试！");
		}
		return this.execute(env);
	}

	/**
	 * 应用菜单新增
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public String doNew(Environment env) throws Exception
	{
		this.sysPageList = CacheManager.getInstance().getCache(ResIcon.class).getCacheData();
		this.icongroup = CacheManager.getInstance().getCache(ResIconGroup.class).getCacheData();
		return "app_menu_edit";
	}

	public String doShow(Environment env) throws Exception
	{
		this.sysPageList = CacheManager.getInstance().getCache(ResIcon.class).getCacheData();
		this.icongroup = CacheManager.getInstance().getCache(ResIconGroup.class).getCacheData();
		this.appmenu = AppMenuDao.doShow(env, domain);
		return this.doNew(env);
	}

	/**
	 * 应用菜单删除
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public String doDelete(Environment env) throws Exception
	{
		String result = AppMenuDao.doDelete(env, domain);
		if ("1".equals(result))
		{
			this.setMessage("删除成功！");
		}
		else
		{
			this.setMessage("删除失败，请重试！");
		}
		return this.execute(env);
	}

	public String doUpdateStatus(Environment env) throws Exception
	{
		String result = AppMenuDao.doUpdateStatus(env, domain);
		if ("1".equals(result))
		{
			this.setMessage("操作成功！");
		}
		else
		{
			this.setMessage("操作失败，请重试！");
		}
		return this.execute(env);
	}

	/**
	 * 重载目录
	 * 
	 * @param env
	 * @throws SQLException
	 */
	public String reloadMumn(Environment env) throws SQLException
	{
		
		env.getLoginUser(LoginUser.class).setUserRoleCode(AppMenuDao.getuserRolecode(env, env.getLoginUser().getUserId()));
		LoginUser user = env.getLoginUser(LoginUser.class);
		env.getRequest().getSession().removeAttribute(SoarProperties.SOAR_BM_LOGINUSER_SESSION_ID);
		env.getRequest().getSession().setAttribute(SoarProperties.SOAR_BM_LOGINUSER_SESSION_ID, user);
		env.getLoginUser(LoginUser.class).reloadAuth(env);
		return "redash";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */

	public AppMenu getModel()
	{
		return this.domain;
	}
}